package com.common.client.map;

import org.gwtopenmaps.openlayers.client.Bounds;
import org.gwtopenmaps.openlayers.client.LonLat;
import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.MapOptions;
import org.gwtopenmaps.openlayers.client.MapWidget;
import org.gwtopenmaps.openlayers.client.event.EventHandler;
import org.gwtopenmaps.openlayers.client.event.EventObject;
import org.gwtopenmaps.openlayers.client.layer.Layer;

import com.common.shared.map.GisMap;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.ui.SimplePanel;
import com.smartgwt.client.widgets.layout.VLayout;

class MapPanel extends VLayout {

	private MapWidget mapWidget;

	public MapWidget getMapWidget() {
		return mapWidget;
	}

	private Map map;
	@SuppressWarnings("unused")
	private int resizetwice = 0;

	private static final String MAP_CENTER = "MAP_CENTER";
	private static final String MAP_ZOOM = "MAP_ZOOM";
	private MapOptions mapOptions;

	public MapPanel(final GisMap gisMap, final MapViewerPanel mapviewer,
			final Boolean without_showinmap, final boolean savecookies) {
		final EventHandler resize = new EventHandler() {

			@Override
			public void onHandle(EventObject eventObject) {
				if (savecookies)
					saveCookies();
			}
		};
		mapOptions = MapHelper.createMapOptions(gisMap);
		mapWidget = new MapWidget("100%", "100%", mapOptions) {
			@Override
			protected void onAttach() {
				super.onAttach();
				Scheduler.get().scheduleDeferred(new Command() {
					@Override
					public void execute() {
						mapWidget.setSize("100%", "100%");
						map = mapWidget.getMap();
						map.addLayers(MapHelper.createLayers(gisMap,
								new Layer[] {}, without_showinmap));
						Bounds bounds = (Bounds) MapHelper.getWraper(
								gisMap.getZoomto(), MapHelper.BOUNDS_PARAMETER);
						map.updateSize();
						if (!getCookies())
							map.zoomToExtent(bounds);
						if (savecookies)
							map.getEvents().register("moveend", map, resize);
						mapviewer.mapCreationComplete();

						mapWidget.setStyleName("map-widget-borderbox");
						// map.zoomTo(14);
						// map.panTo(new LonLat(-99.1, 38.1));
					}
				});
			}
		};

		// map.addControl(new LayerSwitcher());

		// this.setMinWidth(Integer.parseInt(gisMap.getWidth()) - 10);
		// this.addMember(mapWidget);

		setStyleName("map-widget-borderbox");
		setWidth100();
		setHeight100();
		this.addMember(mapWidget);

	}

	public Map getMap() {
		return map;
	}

	@Override
	protected void onLoad() {
		// TODO Auto-generated method stub
		super.onLoad();
	}

	private boolean getCookies() {
		String sMap_center = GWT.getModuleName() + MAP_CENTER;
		String sMap_zoom = GWT.getModuleName() + MAP_ZOOM;
		sMap_center = Cookies.getCookie(sMap_center);
		sMap_zoom = Cookies.getCookie(sMap_zoom);
		int zoom = 0;
		try {
			zoom = Integer.parseInt(sMap_zoom);
			String[] st = sMap_center.split(" ");
			LonLat lat = new LonLat(Double.parseDouble(st[0]),
					Double.parseDouble(st[1]));
			map.setCenter(lat, zoom);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	private void saveCookies() {
		String sMap_center = GWT.getModuleName() + MAP_CENTER;
		String sMap_zoom = GWT.getModuleName() + MAP_ZOOM;
		Cookies.setCookie(sMap_zoom, String.valueOf(map.getZoom()));
		LonLat lat = map.getCenter();
		Cookies.setCookie(sMap_center,
				String.valueOf(lat.lon() + " " + lat.lat()));
	}

	public MapOptions getMapOptions() {
		return mapOptions;
	}

}
